package com.wechat.controller.wechat;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.CallbackIpApi;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.jfinal.weixin.sdk.api.QrcodeApi;
import com.jfinal.weixin.sdk.api.ShorturlApi;
import com.jfinal.weixin.sdk.api.TemplateMsgApi;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;

public class WechatApiController extends ApiController {

	/**
	 * 如果要支持多公众账号，只需要在此返回各个公众号对应的  ApiConfig 对象即可
	 * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
	 */
	public ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();
		
		// 配置微信 API 相关常量
		ac.setToken(PropKit.get("token"));
		ac.setAppId(PropKit.get("appId"));
		ac.setAppSecret(PropKit.get("appSecret"));
		
		/**
		 *  是否对消息进行加密，对应于微信平台的消息加解密方式：
		 *  1：true进行加密且必须配置 encodingAesKey
		 *  2：false采用明文模式，同时也支持混合模式
		 */
		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));
		return ac;
	}
	
	/**
	 * 获取公众号菜单
	 */
	public void getMenu() {
		ApiResult apiResult = MenuApi.getMenu();
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}

	/**
	 * 创建菜单
	 */
	public void createMenu()
	{
		
		StringBuilder menuStr = new StringBuilder();
		menuStr.append("{																				\n");
		menuStr.append("    \"button\": [																\n");
		menuStr.append("        {																		\n");
		menuStr.append("            \"type\": \"view\",													\n");
		menuStr.append("            \"name\": \"商城\",													\n");
		//menuStr.append("            \"url\": \""+PropKit.get("domain")+"/wx\"					    \n");
		menuStr.append("            \"sub_button\": [													\n");
		menuStr.append("            	{																\n");
		menuStr.append("            		\"type\": \"view\",											\n");
        menuStr.append("            		\"name\": \"进入商城\",										\n");
        menuStr.append("            		\"url\": \""+PropKit.get("domain") + "/wx\"				\n" );
		menuStr.append("            	},																\n");
		menuStr.append("            	{																\n");
		menuStr.append("            		\"type\": \"view\",											\n");
		menuStr.append("            		\"name\": \"幸运抽奖\",										\n");
		menuStr.append("            		\"url\": \"https://www.baidu.com\"	\n");
		menuStr.append("            	}																\n");
		menuStr.append("            ]																	\n");
		menuStr.append("        },																		\n");
		menuStr.append("        {																		\n");
		menuStr.append("    		\"type\": \"view\",												    \n");
		menuStr.append("            \"name\": \"个人中心\",												\n");
		menuStr.append("            \"url\": \""+PropKit.get("domain") + "/wx/user\"		\n");
		menuStr.append("        },																		\n");
		menuStr.append("        {																		\n");
		menuStr.append("    		\"type\": \"view\",													\n");
		menuStr.append("    		\"name\": \"关于我们\",												\n");
		menuStr.append("            \"sub_button\": [													\n");
		menuStr.append("            	{																\n");
		menuStr.append("            		\"type\": \"view\",											\n");
        menuStr.append("            		\"name\": \"公司简介\",										\n");
        menuStr.append("            		\"url\": \""+PropKit.get("domain")+"/wx/introduce\"		\n");
		menuStr.append("            	},																\n");
		menuStr.append("            	{																\n");
		menuStr.append("            		\"type\": \"view\",											\n");
		menuStr.append("            		\"name\": \"联系我们\",										\n");
		menuStr.append("            		\"url\": \""+PropKit.get("domain")+"/wx/contact\"		\n");
		menuStr.append("            	}																\n");
		menuStr.append("            ]																	\n");
		menuStr.append("        }																		\n");
		menuStr.append("    ]																			\n");
		menuStr.append("}																				  ");
		
		ApiResult apiResult = MenuApi.createMenu(menuStr.toString());
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}

	/**
	 * 获取公众号关注用户
	 */
	public void getFollowers()
	{
		ApiResult apiResult = UserApi.getFollows();
		renderText(apiResult.getJson());
	}

	/**
	 * 获取用户信息
	 */
	public void getUserInfo()
	{
		ApiResult apiResult = UserApi.getUserInfo("oP6H-t52QVNPUNyjj4glJ3vPiL9k");
		renderText(apiResult.getJson());
	}

	/**
	 * 发送模板消息
	 */
	public void sendMsg()
	{
		String str = " {\n" +
				"           \"touser\":\"olHZwwOXqJZFM-Cdn2cY9TyqTCZM\",\n" +
				"           \"template_id\":\"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY\",\n" +
				"           \"url\":\"http://www.sina.com\",\n" +
				"           \"topcolor\":\"#FF0000\",\n" +
				"           \"data\":{\n" +
				"                   \"first\": {\n" +
				"                       \"value\":\"恭喜你购买成功！\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keyword1\":{\n" +
				"                       \"value\":\"去哪儿网发的酒店红包（1个）\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keyword2\":{\n" +
				"                       \"value\":\"1元\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"remark\":{\n" +
				"                       \"value\":\"欢迎再次购买！\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   }\n" +
				"           }\n" +
				"       }";

		String json = "{\n" +
				"           \"touser\":\"olHZwwOXqJZFM-Cdn2cY9TyqTCZM\",\n" +
				"           \"template_id\":\"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY\",\n" +
				"           \"url\":\"http://weixin.qq.com/download\",            \n" +
				"           \"data\":{\n" +
				"                   \"first\": {\n" +
				"                       \"value\":\"恭喜你购买成功！\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keynote1\":{\n" +
				"                       \"value\":\"巧克力\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keynote2\": {\n" +
				"                       \"value\":\"39.8元\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keynote3\": {\n" +
				"                       \"value\":\"2014年9月22日\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"remark\":{\n" +
				"                       \"value\":\"欢迎再次购买！\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   }\n" +
				"           }\n" +
				"       }";
		ApiResult apiResult = TemplateMsgApi.send(json);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取参数二维码
	 */
	public void getQrcode()
	{
		String str = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}";
		ApiResult apiResult = QrcodeApi.create(str);
		renderText(apiResult.getJson());

//        String str = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"123\"}}}";
//        ApiResult apiResult = QrcodeApi.create(str);
//        renderText(apiResult.getJson());
		
	}
	
	public void createTemporary(){
		/**
		 * {"ticket":"gQGq8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2xVTnBzUHJtRzlWaXFZeHJBRzI2AAIEnhWaVgMEIBwAAA==","expire_seconds":7200,"url":"http:\/\/weixin.qq.com\/q\/lUNpsPrmG9ViqYxrAG26"}
		 */
		ApiResult createTemporary = QrcodeApi.createTemporary(7200, 123);
		renderText(createTemporary.getJson());
	}
	
	public void getShowQrcodeUrl(){
		String url = QrcodeApi.getShowQrcodeUrl("gQGq8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2xVTnBzUHJtRzlWaXFZeHJBRzI2AAIEnhWaVgMEIBwAAA==");
		renderText(url);
	}

	/**
	 * 长链接转成短链接
	 */
	public void getShorturl()
	{
		String str = "{\"action\":\"long2short\"," +
				"\"long_url\":\"http://wap.koudaitong.com/v2/showcase/goods?alias=128wi9shh&spm=h56083&redirect_count=1\"}";
		ApiResult apiResult = ShorturlApi.getShorturl(str);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取客服聊天记录
	 */
	public void getRecord()
	{
		String str = "{\n" +
				"    \"endtime\" : 987654321,\n" +
				"    \"pageindex\" : 1,\n" +
				"    \"pagesize\" : 10,\n" +
				"    \"starttime\" : 123456789\n" +
				" }";
		ApiResult apiResult = CustomServiceApi.getRecord(str);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取微信服务器IP地址
	 */
	public void getCallbackIp()
	{
		ApiResult apiResult = CallbackIpApi.getCallbackIp();
		renderText(apiResult.getJson());
	}
	/**
	 * 添加多客服
	 */
	public void addKfAccount(){
		ApiResult apiResult = CustomServiceApi.addKfAccount("javen@gh_8746b7fa293e", "javen", HashKit.md5("javen123"));
		renderText(apiResult.getJson());
	}
	
	public void sencumsg(){
		ApiResult msg = CustomServiceApi.sendText("oP6H-t52QVNPUNyjj4glJ3vPiL9k", "nihao");
		
	}
	
}
